import wx from './wx'
import constants from '../constants/constants'

export function login() {
  return new Promise((resolve, reject) => {
    wx.login({timeout: 10 * 1000, success: resolve, fail: reject})
  })
}

export function setStorage(key, value) {
  return new Promise((resolve, reject) => {
    wx.setStorage({key: key, data: value, success: resolve, fail: reject})
  })
}

export function getStorage(key) {
  return wx.getStorageSync(key)
}

export function removeStorage(key) {
  return new Promise((resolve, reject) => {
    wx.removeStorage({key: key, success: resolve, fail: reject})
  })
}

export function getLocation(type) {
  return new Promise((resolve, reject) => {
    wx.getLocation({type: type, success: resolve, fail: reject})
  })
}

export function saveUserInfo(value) {
  return new Promise((resolve, reject) => {
    wx.setStorage({key: constants.STORE_CONSTANTS.KEY_USERINFO, data: value, success: resolve, fail: reject})
  })
}

export function saveToken(value) {
  return new Promise((resolve, reject) => {
    wx.setStorage({key: constants.STORE_CONSTANTS.KEY_TOKEN, data: value, success: resolve, fail: reject})
  })
}

export function removeUserInfo() {
  wx.removeStorageSync(constants.STORE_CONSTANTS.KEY_TOKEN)
  wx.removeStorageSync(constants.STORE_CONSTANTS.KEY_USERINFO)
}


export function requestPayment(value) {
  return new Promise((resolve, reject) => {
    value.success = resolve
    value.fail = reject
    wx.requestPayment(value)
  })
}

